using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;

namespace ModWatch
{

	/// <summary>
	/// An implementaion of <see cref="System.Web.UI.WebControls.CheckBox"/>
	/// that notifies the user if a change has been made to any
	/// <see cref="ModWatch.IModWatcher"/> objects.
	/// </summary>
	[Obsolete("The recommended alternative is ModWatch.OnCheckBoxChangedModAlertExtender", true)]
	public class ModAlertCheckBox : CheckBox, IScriptControl
	{

		/// <summary>
		/// Gets or sets the modification group to be checked for changes.
		/// </summary>
		public string ModGroup
		{
			get { return (string)ViewState["ModGroup"]; }
			set { ViewState["ModGroup"] = value; }
		}

		/// <summary>
		/// Gets or sets the dialog message that should prompt the user if a change exists.
		/// </summary>
		public string NotifyModMessage
		{
			get { return (string)ViewState["NotifyModMessage"]; }
			set { ViewState["NotifyModMessage"] = value; }
		}

		/// <summary>
		/// Adds the HTML attributes and styles of a <see cref="ModAlertCheckBox"/>
		/// control to be rendered to the specified output stream.
		/// </summary>
		/// <param name="writer">
		/// An <see cref="System.Web.UI.HtmlTextWriter">HtmlTextWriter</see>
		/// that represents the output stream to render HTML content on the client.
		/// </param>
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			string onclick = null;
			if (base.Attributes != null)
			{
				onclick = base.Attributes["onclick"];
				if (onclick != null && onclick.Trim().Length > 0)
				{
					base.Attributes.Remove("onclick");
				}
			}

			StringBuilder newOnClick = new StringBuilder();
			newOnClick.Append("if ($find('");
			newOnClick.Append(this.ClientID);
			newOnClick.Append("').checkForChange())");
			newOnClick.Append(" return false;");
			newOnClick.Append(onclick);

			base.Attributes.Add("onclick", newOnClick.ToString());
		}

		/// <summary>
		/// Raises the <see cref="System.Web.UI.Control.PreRender"/> event.
		/// </summary>
		/// <param name="e">
		/// An <see cref="System.EventArgs"/> object that contains the event data.
		/// </param>
		protected override void OnPreRender(EventArgs e)
		{
			if (!this.DesignMode)
			{
				// Test for ScriptManager and register if it exists
				ScriptManager sm = ScriptManager.GetCurrent(Page);

				if (sm == null)
					throw new HttpException("A ScriptManager control must exist on the current page.");

				sm.RegisterScriptControl(this);
			}

			base.OnPreRender(e);
		}

		/// <summary>
		/// Displays the <see cref="ModWatch.ModAlertCheckBox"/>
		/// on the client.
		/// </summary>
		/// <param name="writer"></param>
		protected override void Render(HtmlTextWriter writer)
		{
			if (!this.DesignMode)
			{
				ScriptManager sm = ScriptManager.GetCurrent(Page);
				sm.RegisterScriptDescriptors(this);
			}

			base.Render(writer);
		}

		#region IScriptControl Members

		/// <summary>
		/// Returns the <see cref="System.Web.UI.ScriptDescriptor"/>s required by
		/// this instance of <see cref="ModWatch.ModAlertCheckBox"/>.
		/// </summary>
		/// <returns>
		/// A collection of <see cref="System.Web.UI.ScriptDescriptor"/>s
		/// defining this instance of <see cref="ModWatch.ModAlertCheckBox"/>.
		/// </returns>
		public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
		{
			ScriptControlDescriptor scd = new ScriptControlDescriptor("ModWatch.ModAlertCheckBox", this.ClientID);

			scd.AddProperty("modGroup", this.ModGroup);
			scd.AddProperty("notifyModMessage", this.NotifyModMessage);

			return new ScriptDescriptor[] { scd };
		}

		/// <summary>
		/// Returns the <see cref="ModWatch.ModWatchExtenderBase.GettingScriptReferences"/>
		/// objects needed by the <see cref="ModWatch.ModAlertCheckBox"/>.
		/// </summary>
		/// <returns>
		/// The <see cref="ModWatch.ModWatchExtenderBase.GettingScriptReferences"/>
		/// objects needed by the <see cref="ModWatch.ModAlertCheckBox"/>.
		/// </returns>
		public IEnumerable<ScriptReference> GetScriptReferences()
		{
			ScriptReference sr = new ScriptReference();
			sr.Path = Page.ClientScript.GetWebResourceUrl(typeof(ModAlertCheckBoxList), "ModWatch.Scripts.ModAlertCheckBox.js");

			return new ScriptReference[] { sr };
		}

		#endregion
	}

}
